package api

import (
	"encoding/json"
	"io/ioutil"
	"net/http"

	"rt.com/mocktool/mock"
	"rt.com/mocktool/utils/glog"
)

// DeleteMethod 要删除的方法集合
type DeleteMethod struct {
	DeleteList []mock.MethodItem `json:"deleteList"`
}

// deleteMethods 删除选中的Mock方法
func deleteMethods(w http.ResponseWriter, r *http.Request) {
	debugAPI(w, r)
	// 解析body体
	body, err := ioutil.ReadAll(r.Body)
	r.Body.Close()
	if err != nil {
		glog.Error(err)
		errResp, _ := json.Marshal(ErrorMsg{
			Msg:  err.Error(),
			Code: 100,
		})
		w.Write(errResp)
		return
	}
	// 解析json
	result := DeleteMethod{}
	err = json.Unmarshal(body, &result)
	if err != nil {
		glog.Error(err)
		errResp, _ := json.Marshal(ErrorMsg{
			Msg:  err.Error(),
			Code: 100,
		})
		w.Write(errResp)
		return
	}
	// 删除
	mock.Instance.DeleteMethod(result.DeleteList)
	// 响应成功
	successResp, _ := json.Marshal(ErrorMsg{
		Msg:  "success",
		Code: 0,
	})
	// 写响应
	w.Write(successResp)
}
